Ext.namespace("NewCustomer");

NewCustomer.NewCustomer = function(p) {
	var ps = new ProjectSourceDemo();
	var userSelector = null;
	//增加
	
	
	this.customerName = new Ext.form.TextField({
		fieldLabel : '客户名称',
		width : 180,
		allowBlank : false
	});
		
	this. customerArea = new Ext.form.ComboBox( {
		fieldLabel : '所属区域',
		store: OMS.AREA,
		mode : 'local',
		displayField : 'name',
		triggerAction : 'all',
		editable : false,
		valueField : 'id',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true
	});
	this.renderprojectType = function(value){
		return OMS.PROJECTTYPEOBJECT[value];
	};
		
	this. SuccessTime = new Ext.form.DateField( {
		fieldLabel : '预计签约时间',
		
		format : 'Y-m-d',
		width : 150
	});
	
	var leader = new Ext.form.TextField({
		fieldLabel : '参与领导',
		width : 150,
		listeners: {
			   'render': function(cmp) {
					var self = cmp.ownerCt.ownerCt;
					
					cmp.getEl().on('click', function(){showUser1();}, self); 
				}
			}
	});
	var showUser1 = function(){
		//var scope=this;
		selectUserId = [129,3840,20151];
		
		if(userSelector==null){
			userSelector=new Tree.User(selectUserId,function(o){
				//scope.setUsers1(o);
				setUsers1(o);
			},null,'105');
		}
		userSelector.show();
	};
	var setUsers1 = function(o){
		var names = [];
		var scope=this;
		selectUserId =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			selectUserId.push(o[i].id);
		}
		leader.setValue(names.join(';'));
	};
	
	this. CustomerAddress = new Ext.form.TextField({
		fieldLabel : '公司地址',
		width : 180,
		allowBlank : false
	});
	
	
	
	this. COMPANYSIZE = new Ext.form.ComboBox( {
		fieldLabel : '公司规模',
		store: OMS.COMPANYSIZE,
		mode : 'local',
		displayField : 'name',
		triggerAction : 'all',
		editable : false,
		valueField : 'id',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true
	});
	
	
	this. FirstContact = new Ext.form.TextField({
		fieldLabel : '第1联系人',
		width : 150
	});
	
	this. CompanyPhone = new Ext.form.TextField({
		fieldLabel : '办公电话',
		width : 150
	});
	
	this. Mobile = new Ext.form.TextField({
		fieldLabel : '手机',
		width : 150
	});
	
	this. connEmail = new Ext.form.TextField({
		fieldLabel : 'EMAIL',
		width : 150,
		vtype : 'email'
	});
	
	this. SecondContact = new Ext.form.TextField({
		fieldLabel : '第2联系人名',
		width : 150
	});
	
	this. SecondMobile = new Ext.form.TextField({
		fieldLabel : '第2联系人电话',
		width : 150
	});
	
	this. qq = new Ext.form.TextField({
		fieldLabel : 'qq',
		width : 150
	});
	
	this. msn = new Ext.form.TextField({
		fieldLabel : 'msn',
		width : 150
	});
	this. fax = new Ext.form.TextField({
		fieldLabel : 'fax',
		width : 150
	});
	this. ContactLike = new Ext.form.TextField({
		fieldLabel : '爱好',
		width : 150
	});
	
	
	this. Birthday = new Ext.form.DateField( {
		fieldLabel : '第1联系人生日',
		format : 'Y-m-d',
		width : 150
	});
	this. SALESTAGE = new Ext.form.ComboBox( {
		fieldLabel : '销售阶段',
		store: OMS.SALESTAGE,
		mode : 'local',
		displayField : 'name',
		triggerAction : 'all',
		editable : false,
		valueField : 'id',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true
	});
	
	
	
	this. PhoneMoney = new Ext.form.NumberField({
		fieldLabel : '电话金额',
		width : 150,
		value:"0"
	});
	this. MailMoney = new Ext.form.NumberField({
		fieldLabel : '邮箱金额',
		width : 150,
		value:"0"
	});
	this. netMoney = new Ext.form.NumberField({
		fieldLabel : '网络金额',
		width : 150,
		value:"0"
	});
	
	// 竞争对手信息---begin
	this. VsName = new Ext.form.TextField({
		fieldLabel : '竞争对手名称',
		width : 180
	});
	
	this. VsManager = new Ext.form.TextField({
		fieldLabel : '项目负责人',
		width : 150
	});
	
	this. VsTime = new Ext.form.DateField( {
		fieldLabel : '介入时间',
		format : 'Y-m-d',
		width : 150
	});
	
	this. VsGood = new Ext.form.TextArea({
		fieldLabel : '优势',
		width : 250
	});
	
	this. VsBad = new Ext.form.TextArea({
		fieldLabel : '劣势',
		width : 250
	});
	
	
	this.parentWin=p;	
	this.winMsg=null;
	this.temp=0;
	
	this.txtRemind=new Ext.form.Hidden({name:'remind'});
	this.hdSuperviseId = new Ext.form.Hidden({name:'superviseId'});
	this.txtSuperviseTitle = new Ext.form.TextField({
		fieldLabel: '问题标题',
		name:'superviseTitle',		
		maxLength:100,
		width: 380
	});
	
	this.txtManagerId=new Ext.form.Hidden({name:'managerId'});
	this.txtManagerName = new Ext.form.Hidden({name:'managerName'});
	this.txtReportDept=new Ext.form.Hidden({name:'reportDept'});
	this.txtReportDeptName = new Ext.form.TextField({
		width: 150,
		fieldLabel: '责任部门',
		name:'reportDeptName',
		readOnly: true
	});
	
	this.txtSuperviseContent = new Ext.form.TextArea({
		fieldLabel: '资料描述',
		name:'superviseContent',
		maxLength:1000,
		blankText:'请输入资料描述',
		allowBlank:false,
		height:60,
		width: 380
	});
	
	this.typeStore  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.FILE_TYPE2
    });
	this.txtIssueType = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '资料类别',
		name:'issueType',
		store: this.typeStore,
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.txtIssueType.on('select', this.changeType, this);	
	this.dtIssueDate = new Ext.form.DateField({
    	fieldLabel: '上传时间',
    	name:'issueDate',
		width: 150,
		format: 'Y-m-d'
	});
	this.typeLebel  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.FILE_LEVEL2
    });
	this.txtInstancyLevel = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '重要程度',
		name:'instancyLevel',
		store: this.typeLebel,
		value:"0",	
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.form = new Ext.FormPanel({
		frame : true,
		bodyStyle: 'padding: 5px 5px 0',
		width: 800,
		labelWidth: 70,
        labelAlign: 'right',
        items: [{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.customerName]},
	        	         { columnWidth: .33, items: [this.customerArea]},
	        	         { columnWidth: .33, items: [leader]}
	        	         ]},
	        	{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.CustomerAddress]},
	        	         { columnWidth: .33, items: [this.COMPANYSIZE]},
	        	         { columnWidth: .33, items: [this.FirstContact]}
	        	         ]},
	        	{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.CompanyPhone]},
	        	         { columnWidth: .33, items: [this.Mobile]},
	        	         { columnWidth: .33, items: [this.connEmail]}
	        	         ]},
	    		{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.qq]},
	        	         { columnWidth: .33, items: [this.msn]},
	        	         { columnWidth: .33, items: [this.fax]}
	        	         ]},
	    		{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.ContactLike]},
	        	         { columnWidth: .33, items: [this.Birthday]},
	        	         { columnWidth: .33, items: [this.SecondContact]}
	        	         ]},
	    		{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.SecondMobile]},
	        		 	 { columnWidth: .33, items: [this.SALESTAGE]},
	        		 	 { columnWidth: .33, items: [this.SuccessTime]}
	        		 	 
	        	         ]},
	    		{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.PhoneMoney]},
	        			 { columnWidth: .33, items: [this.MailMoney]},
	        			 { columnWidth: .33, items: [this.netMoney]}
	        	         ]},
	    		{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .33, items: [this.VsName]},
	        			 { columnWidth: .33, items: [this.VsManager]},
	        			 { columnWidth: .33, items: [this.VsTime]}
	        	         ]},
	        	{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .5, items: [this.VsGood]},
	        			 { columnWidth: .5, items: [this.VsBad]}
	        	         ]}
	    	
	    	]
    });
    this.fileForm = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        width: 800,
        labelWidth: 70,
        labelAlign: 'right',
        fileUpload: true, 
        items: []
	});
    this.btnSave = new Ext.Button({
    	text:'保存',
    	handler:this.onSave,
    	scope: this
    });
    this.btnSubmit = new Ext.Button({
    	text:'提交',
    	handler:this.onSubmit,
    	scope: this
    });
	NewCustomer.NewCustomer.superclass.constructor.call(this, {
		title: '客户信息',
        autoHeight: true,
        width: 800,
        y: 10,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: false,
        closeAction: 'hide',
        items: [this.form,this.fileForm],
        buttons: [this.btnSave,
    	/**
    	this.btnSubmit,
        {
			text: '添加附件',
			iconCls: 'icon-new',
			handler: this.addFile,
			scope: this
        },**/
        {
            text: '关闭',
            scope: this,
            handler: function() {
	        	this.hide();
        	}
        }],
        buttonAlign:'center'
	});
};
Ext.extend(NewCustomer.NewCustomer, Ext.Window, {
	initWin:function(data){
		//alert(Ext.util.JSON.encode(data));
		if(data){
			this.form.getForm().setValues(data);
			this.initFile(data);
		}else{
			this.form.getForm().reset();
			this.fileForm.removeAll(true);
			this.dtIssueDate.setValue(new Date());
		}			
	},
	initFile:function(o){
		this.fileForm.removeAll(true);
		var conn = new Ext.data.Connection({
			url:'../superviseIssue/searchUpLoadFiles.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		})
		Utils.request(conn,{
			params:{superviseId:o.superviseId},
			scope:this,
			success:function(data){
				var files=data.result;
				for(var i=0;i<files.length;i++){
					this.scope.addFile(files[i]);
				}
			}
		});
	},
	addFile:function(o){
		this.temp+=1;
		var fname="fileName"+this.temp;
		var hname="hideFileId"+this.temp;
		var fileText=(o.ACCESSORY_NAME==null?"请选择要上传附件":o.ACCESSORY_NAME);
		var fileField=new Ext.form.FileUploadField({
	    	fieldLabel: '上传附件',
			emptyText: fileText,
			width : 220,
			name : fname,
			buttonText: '',
			buttonCfg: {
	            iconCls: 'icon-compress'
	        }
	    });
	    var fileDel=new Ext.Button({
	    	text: '清除',
	    	iconCls: 'icon-remove',
	    	scope: this,
	    	handler: this.delFile
	    });
	    if(o.ACCESSORY_ID){
		    var hdFile=new Ext.form.Hidden({
		    	name:hname,
		    	value:o.ACCESSORY_ID
		    });
		    var href='../superviseIssue/downloadFile.form?fileId='+o.ACCESSORY_ID+'&timesnap=' + Date.parse(new Date());
			var lbLink=new Ext.form.Label({
				html:'<a href="'+href+'">'+o.ACCESSORY_NAME+'</>',
				width:280
			});			
			var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .86, items: [fileField]},
		        	         { columnWidth: .14, items: [fileDel] },
		        	         { columnWidth: 1, items: [lbLink] },
		        	         hdFile]
		    };
	    }else{
	    	var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .86, items: [fileField]},
		        	         { columnWidth: .14, items: [fileDel] }
		        	       ]
		    };
	    }   	
		this.fileForm.add(item);
	    this.fileForm.doLayout();
	},
	delFile:function(btn,event){
		var item=btn.ownerCt.ownerCt;
		this.fileForm.remove(item,true);
	    this.fileForm.doLayout();
	},
	changeType:function(combo,record,index){
		var v=this.txtIssueType.getValue();
		if(v=='7'||v=='8'||v=='9'){
			this.txtReportDept.setValue('2177');
			this.txtReportDeptName.setValue('综合部');
			this.txtManagerId.setValue('20155');
			this.txtManagerName.setValue('刘琪');
		}else{
			this.txtReportDept.setValue('2178');
			this.txtReportDeptName.setValue('市场部');
			this.txtManagerId.setValue('23421');
			this.txtManagerName.setValue('郭超');
		}
	},
	onSave: function() {
		this.saveIssue('0');
	},
	onSubmit: function() {
		scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.txtRemind.setValue(scope.winMsg.txtRemind);
				scope.submitIssue();
			},'提交后将不可修改，你确实要提交吗？');
		this.winMsg.show();
	},
	submitIssue:function(){
		this.saveIssue('1');
	},
	saveIssue: function(state) {
		if(!this.form.getForm().isValid()){
			Utils.info2("有必填项没有填写，请检查！",180);
			return;
		}
			var url='../customer/addCustomer.form?timesnap=' + Date.parse(new Date());
			var param=this.form.getForm().getFieldValues();
			alert(param);
			Utils.formatDate(param,'issueDate');
			param['superviseState']=state;
			scope=this;
			thiz=this;
			pp=this;
			this.fileForm.getForm().submit({
				waitTitle : '系统提示',
				waitMsg : '数据提交中，请稍候...',
				url : url,
				params : {
					CustomerName : scope.customerName.getValue(),
					Birthday    :scope.Birthday.getValue()? scope.Birthday.getValue().format('Y-m-d') : '',
					SuccessTime :scope.SuccessTime? thiz.SuccessTime.getValue().format('Y-m-d') : '',
					VsTime      :scope.VsTime?  pp.VsTime.getValue().format('Y-m-d') : '',
					
					customerArea :scope.customerArea.getValue(),
					
					leader:leader.getValue(),
					
					CustomerAddress:scope.CustomerAddress.getValue(),
					COMPANYSIZE:scope.COMPANYSIZE.getValue(),
					FirstContact:scope.FirstContact.getValue(),
					CompanyPhone:scope.CompanyPhone.getValue(),
					Mobile:scope.Mobile.getValue(),
					qq:scope.qq.getValue(),
					msn:scope.msn.getValue(),
					Email:scope.connEmail.getValue(),
					fax:scope.fax.getValue(),
					
					ContactLike:scope.ContactLike.getValue(),
					SecondContact:scope.SecondContact.getValue(),
					SecondMobile:scope.SecondMobile.getValue(),
					PhoneMoney:scope.PhoneMoney.getValue(),
					MailMoney:scope.MailMoney.getValue(),
					netMoney:scope.netMoney.getValue(),
					
					
					
					SALESTAGE:scope.SALESTAGE.getValue(),
					VsName:scope.VsName.getValue(),
					VsManager:scope.VsManager.getValue(),
					
					VsGood:scope.VsGood.getValue(),
					VsBad:scope.VsBad.getValue(),
					Stage:'1'
				},
				method : 'POST',			
				success : function(fp, o){
					Utils.info2();
			  		scope.hide();			
					scope.parentWin.queryIssue();
				},
				failure : function(fp, o){
					alert('error');
				}
			});  
		}		
	
});
